package com.godpaper.twoHitOne.busniess.factory
{
	import com.godpaper.twoHitOne.events.UntoggleOtherGasketsEvent;
	import com.godpaper.twoHitOne.events.UntoggleOtherPiecesEvent;
	import com.godpaper.twoHitOne.model.BoardModel;
	import com.godpaper.twoHitOne.model.PiecesModel;
	import com.godpaper.twoHitOne.views.ChessBoard;
	import com.godpaper.twoHitOne.views.components.*;
	
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	
	public class ButtonFactory extends EventDispatcher
	{
		public static var chessBoard:ChessBoard;
		private static var iPieceButton:IFlexDisplayObject;
		public static const FLAG_RED:String = "red";
		public static const FLAG_BLUE:String = "blue";
		private static const MAX_PIECE_NUM:int = 6;
		
		public function ButtonFactory()
		{
			//TODO: implement function
		}
		public static const TYPE_PIECE:String = "pieceButton";
		public static const TYPE_GASKET:String = "gasketButton";
		
		public static function produce(type:String,boardTarget:ChessBoard,flagColor:String=null):void
		{
			//restore board;
			chessBoard = boardTarget;
			switch(type)
			{
				case TYPE_PIECE:
					
					if(flagColor==FLAG_RED)
					{
						//Postive RED
						for(var i:int=0;i<MAX_PIECE_NUM;i++)
						{
							var pieceButtonRED:PieceButton = new PieceButton();
//			iPieceButton = PopUpManager.createPopUp(chessBoard,PieceButton);
//			PopUpManager.centerPopUp(iPieceButton);
							pieceButtonRED.styleName = PieceButton.BUTTON_STYLE_NAME_RED;
							pieceButtonRED.name = pieceButtonRED.pieceVO.name = PieceButton.NAME_PREFIX_RED.concat(i.toString());
							pieceButtonRED.label = i.toString();
							pieceButtonRED.enabled = false;
							switch(i)
							{
								case 0:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2;
									pieceButton.y = chessBoardPoint.y -pieceButton.height/2;*/
									pieceButtonRED.x = -pieceButtonRED.width/2+ChessBoard.PADDING;
									pieceButtonRED.y = -pieceButtonRED.height/2+ChessBoard.PADDING;
									pieceButtonRED.pieceVO.position = [0,0];
									BoardModel.getInstance().gamePosition.sett(0,0,pieceButtonRED);
									break;
								case 1:
								case 2:
								case 3:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2 + chessBoard.vlRepeater.moveOffsetX*i;
									pieceButton.y = chessBoardPoint.y -pieceButton.height/2;*/
									pieceButtonRED.x = -pieceButtonRED.width/2 + chessBoard.vlRepeater.moveOffsetX*i+ChessBoard.PADDING;
									pieceButtonRED.y = -pieceButtonRED.height/2+ChessBoard.PADDING;
									pieceButtonRED.pieceVO.position = [i,0];
									BoardModel.getInstance().gamePosition.sett(i,0,pieceButtonRED);
									break;
								case 4:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2;
									pieceButton.y = chessBoardPoint.y -pieceButton.height/2 + chessBoard.hlRepeater.moveOffsetY;*/
									pieceButtonRED.x = -pieceButtonRED.width/2+ChessBoard.PADDING;
									pieceButtonRED.y = -pieceButtonRED.height/2 + chessBoard.hlRepeater.moveOffsetY+ChessBoard.PADDING;
									pieceButtonRED.pieceVO.position = [0,1];
									BoardModel.getInstance().gamePosition.sett(0,1,pieceButtonRED);
									break;
								case 5:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2 + chessBoard.vlRepeater.moveOffsetX*3;
									pieceButton.y = chessBoardPoint.y -pieceButton.height/2 + chessBoard.hlRepeater.moveOffsetY;*/
									pieceButtonRED.x = -pieceButtonRED.width/2 + chessBoard.vlRepeater.moveOffsetX*3+ChessBoard.PADDING;
									pieceButtonRED.y = -pieceButtonRED.height/2 + chessBoard.hlRepeater.moveOffsetY+ChessBoard.PADDING;
									pieceButtonRED.pieceVO.position = [3,1];
									BoardModel.getInstance().gamePosition.sett(3,1,pieceButtonRED);
									break;
								default:
									break;	
							}
							//add to chessBoard
							chessBoard.addChild(pieceButtonRED);
							//push to collection;
							PiecesModel.getInstance().redPiecesCollection.addItem(pieceButtonRED);
							
						}
						
					}else
					{
						//Nagetive BLUE
						for(var j:int=0;j<MAX_PIECE_NUM;j++)
						{
							var pieceButtonBLUE:PieceButton = new PieceButton();
//							iPieceButton = PopUpManager.createPopUp(chessBoard,PieceButton);
//							PopUpManager.centerPopUp(iPieceButton);
							pieceButtonBLUE.styleName = PieceButton.BUTTON_STYLE_NAME_BLUE;
							pieceButtonBLUE.name = pieceButtonBLUE.pieceVO.name = PieceButton.NAME_PREFIX_BLUE.concat(j.toString());
							pieceButtonBLUE.label = j.toString();
							switch(j)
							{
								case 2:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2;
									pieceButton.y = chessBoardPoint.y+chessBoard.height -pieceButton.height/2;*/
									pieceButtonBLUE.x = -pieceButtonBLUE.width/2+ChessBoard.PADDING;
									pieceButtonBLUE.y = chessBoard.height -pieceButtonBLUE.height/2-ChessBoard.PADDING;
									pieceButtonBLUE.pieceVO.position = [0,3];
									BoardModel.getInstance().gamePosition.sett(0,3,pieceButtonBLUE);
									break;
								case 3:
								case 4:
								case 5:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2 + chessBoard.vlRepeater.moveOffsetX*(j-2);
									pieceButton.y = chessBoardPoint.y+chessBoard.height -pieceButton.height/2;*/
									pieceButtonBLUE.x = -pieceButtonBLUE.width/2 + chessBoard.vlRepeater.moveOffsetX*(j-2)+ChessBoard.PADDING;
									pieceButtonBLUE.y = chessBoard.height -pieceButtonBLUE.height/2-ChessBoard.PADDING;
									pieceButtonBLUE.pieceVO.position = [j-2,3];
									BoardModel.getInstance().gamePosition.sett(j-2,3,pieceButtonBLUE);
									break;
								case 0:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2;
									pieceButton.y = chessBoardPoint.y+chessBoard.height -pieceButton.height/2 - chessBoard.hlRepeater.moveOffsetY;*/
									pieceButtonBLUE.x = -pieceButtonBLUE.width/2+ChessBoard.PADDING;
									pieceButtonBLUE.y = chessBoard.height -pieceButtonBLUE.height/2 - chessBoard.hlRepeater.moveOffsetY-ChessBoard.PADDING;
									pieceButtonBLUE.pieceVO.position = [0,2];
									BoardModel.getInstance().gamePosition.sett(0,2,pieceButtonBLUE);
									break;
								case 1:
									/*pieceButton.x = chessBoardPoint.x -pieceButton.width/2 + chessBoard.vlRepeater.moveOffsetX*3;
									pieceButton.y = chessBoardPoint.y+chessBoard.height -pieceButton.height/2 - chessBoard.hlRepeater.moveOffsetY;*/
									pieceButtonBLUE.x = -pieceButtonBLUE.width/2 + chessBoard.vlRepeater.moveOffsetX*3+ChessBoard.PADDING;
									pieceButtonBLUE.y = chessBoard.height -pieceButtonBLUE.height/2 - chessBoard.hlRepeater.moveOffsetY-ChessBoard.PADDING;
									pieceButtonBLUE.pieceVO.position = [3,2];
									BoardModel.getInstance().gamePosition.sett(3,2,pieceButtonBLUE);
									break;
								default:
									break;	
							}
							//add to chessBoard
							chessBoard.addChild(pieceButtonBLUE);
							//push to collection;
							PiecesModel.getInstance().bluePiecesCollection.addItem(pieceButtonBLUE);
						}
						trace("initialized gamePosition:",BoardModel.getInstance().gamePosition.dump());
					}
					break;
				case TYPE_GASKET:
					for(var ii:int=0;ii<16;ii++)
					{
						var gasketButton:GasketButton = new GasketButton();
						gasketButton.name = GasketButton.NAME_PREFIX.concat(ii.toString());
						gasketButton.label = ii.toString();
						gasketButton.enabled = false;
						switch(ii)
						{
							case 0:
								gasketButton.x = -gasketButton.width/2+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2+ChessBoard.PADDING;
								gasketButton.position = [0,0];
								break;
							case 1:
							case 2:
							case 3:
								gasketButton.x = -gasketButton.width/2 + chessBoard.vlRepeater.moveOffsetX*ii+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2+ChessBoard.PADDING;
								gasketButton.position = [ii,0];
								break;
							case 4:
								gasketButton.x = -gasketButton.width/2+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY+ChessBoard.PADDING;
								gasketButton.position = [0,1];
								break;
							case 5:
							case 6:
							case 7:
								gasketButton.x = -gasketButton.width/2 + chessBoard.vlRepeater.moveOffsetX*(ii%4)+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY+ChessBoard.PADDING;
								gasketButton.position = [ii-4,1];
								break;
							case 8:
								gasketButton.x = -gasketButton.width/2+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY*2+ChessBoard.PADDING;	
								gasketButton.position = [0,2];
								break;
							case 9:
							case 10:
							case 11:
								gasketButton.x = -gasketButton.width/2 + chessBoard.vlRepeater.moveOffsetX*(ii%4)+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY*2+ChessBoard.PADDING;
								gasketButton.position = [ii-8,2];
								break;
							case 12:
								gasketButton.x = -gasketButton.width/2+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY*3+ChessBoard.PADDING;		
								gasketButton.position = [0,3];
								break;
							case 13:
							case 14:
							case 15:
								gasketButton.x = -gasketButton.width/2 + chessBoard.vlRepeater.moveOffsetX*(ii%4)+ChessBoard.PADDING;
								gasketButton.y = -gasketButton.height/2 + chessBoard.hlRepeater.moveOffsetY*3+ChessBoard.PADDING;
								gasketButton.position = [ii-12,3];
								break;					
							default:
								break;	
						}
						
						chessBoard.rawChildren.addChild(gasketButton);
						PiecesModel.getInstance().gasketCollection.addItem(gasketButton);
					}	
					break;
				default:
					break;		
			}
			
			
		}
		
		protected static function get chessBoardPoint():Point
		{
			return Application.application.localToGlobal(new Point(chessBoard.x,chessBoard.y));
		}
		
		protected static function get pieceButton():PieceButton
		{
			return new PieceButton();
//			return iPieceButton as PieceButton;
		}
		
	}
}